import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login/login.vue'
import Home from '../views/Home/home.vue'
import Relic from '../views/relic/relic.vue'
import Person from '../views/person/person.vue'
import personCollectionPage from '../views/person/personPages/personCollectionPage.vue'
import personManage from '../views/person/personPages/personManage.vue'
import list from '../views/list/list.vue'
import register from '../views/register/register.vue'
import AdminHome from '../views/admin/Home/home.vue'
import SuperAdminHome from '../views/superAdmin/home/home.vue'
import AdminPerson from '../views/admin/AdminPerson/adminPerson.vue'
import SuperAdminPerson from '../views/superAdmin/SuperAdminPerson/SuperAdminPerson.vue'
import store from '@/store'
import {  Message} from 'element-ui';


const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)
//创建router实例对象，去管理一组一组的路由规则
const router = new VueRouter({
	mode:'history',
	routes:[
		{
			path:'/',
			name:'login',
			component:login,
		},{
			name:'register',
			path:'/register',
			component:register
		},
		{
			name:'Home',
			path:'/Home',
			component:Home,
			meta: { role: "user" } //需要认证的路由
		},
		{
			name:'Person',
			path:'/Person',
			component:Person,
			meta: { role: "user" }, //需要认证的路由
			children: [
				{
					path:'personCollectionPage',
					name:'personCollectionPage',
					component:personCollectionPage,
					meta: { role: "user" } //需要认证的路由
				},
				{
					path:'personManage',
					name:'personManage',
					component:personManage,
					meta: { role: "user" } ,//需要认证的路由
					children:[
						{
							path:'/',
							redirect:"baseData"
						},
						{
							path: 'baseData',
							name: 'baseData',
							meta: { role: "user" },
							component: () => import('../views/person/component/baseData.vue'),
						},
						{
							path: 'changeImage',
							name: 'changeImage',
							meta: { role: "user" },
							component: () => import('../views/person/component/changeImage.vue'),
						},
						{
							path: 'passwordManage',
							name: 'passwordManage',
							meta: { role: "user" },
							component: () => import('../views/person/component/passwordManage.vue'),
						},
					]
				}
			]

		},
		{
			name:'AdminPerson',
			path:'/AdminPerson',
			component:AdminPerson,
			meta: { role: "admin" }, //需要认证的路由
			children:[
				{
					path:'/',
					redirect:"baseData"
				},
				{
					path: 'baseData',
					name: 'baseData',
					meta: { role: "admin" },
					component: () => import('../views/person/component/baseData.vue'),
				},
				{
					path: 'changeImage',
					name: 'changeImage',
					meta: { role: "admin" },
					component: () => import('../views/person/component/changeImage.vue'),
				},
				{
					path: 'passwordManage',
					name: 'passwordManage',
					meta: { role: "admin" },
					component: () => import('../views/person/component/passwordManage.vue'),
				},
			]
		},
		{
			name:'smartList',
			path:'/smartList',
			component: () => import('../views/smartList/smartList.vue'),
			meta: { role: "user" } //需要认证的路由
		},
		{
			name:'list',
			path:'/list',
			component:list,
			meta: { role: "user" } //需要认证的路由
		},
		{
			name:'Relic',
			path:'/Relic',
			component:Relic,
			meta: { role: "user" } //需要认证的路由
		},
		{
			name:'SuperAdminPerson',
			path:'/SuperAdminPerson',
			component:SuperAdminPerson,
			meta: { role: "superAdmin" }, //需要认证的路由
			children:[
				{
					path:'/',
					redirect:"baseData"
				},
				{
					path: 'baseData',
					name: 'baseData',
					meta: { role: "superAdmin" },
					component: () => import('../views/person/component/baseData.vue'),
				},
				{
					path: 'changeImage',
					name: 'changeImage',
					meta: { role: "superAdmin" },
					component: () => import('../views/person/component/changeImage.vue'),
				},
				{
					path: 'passwordManage',
					name: 'passwordManage',
					meta: { role: "superAdmin" },
					component: () => import('../views/person/component/passwordManage.vue'),
				},
			]
		},
		{
			name:'SuperAdminHome',
			path:'/SuperAdminHome',
			component:SuperAdminHome,
			meta: { role: "superAdmin" },
			children: [
				{
					path:'/',
					redirect:"Role"
				},
				{
					path: 'Role',
					name: 'Role',
					meta: { role: "superAdmin" },
					component: () => import('../views/superAdmin/Role/role.vue'),
					children: [
						{
							path:'/',
							redirect:"RoleManage"
						},
						{
							path: 'RoleManage',
							name: 'RoleManage',
							meta: { role: "superAdmin" },
							component: () => import('../views/superAdmin/Role/RoleManage.vue'),
						},
					]
				},
				{
					path: 'AdminManage',
					name: 'AdminManage',
					meta: { role: "superAdmin" },
					component: () => import('../views/superAdmin/AdminManage/AdminManage.vue'),
					children: [
						{
							path:'/',
							redirect:"adminList"
						},
						{
							path: 'adminList',
							name: 'adminList',
							meta: { role: "superAdmin" },
							component: () => import('../views/superAdmin/AdminManage/adminList.vue'),
						},
						{
							path: 'adminAdd',
							name: 'adminAdd',
							meta: { role: "superAdmin" },
							component: () => import('../views/superAdmin/AdminManage/adminAdd.vue'),
						},{
							path: 'adminUpdate',
							name: 'adminUpdate',
							meta: { role: "superAdmin" },
							component: () => import('../views/superAdmin/AdminManage/adminUpdate.vue'),
						},
					]
				}
			]
		},

		{
			name:'AdminHome',
			path:'/AdminHome',
			component:AdminHome,
			meta: { role: "admin" },
			children: [
				{
					path:'/',
					redirect:"AdminRelic"
				},
				{
					path: 'AdminRelic',
					name: 'AdminRelic',
					meta: { role: "admin" },
					component: () => import('../views/admin/Relic/relic.vue'),
					children: [
						{
							path:'/',
							redirect:"relicList"
						},
						{
							path: 'relicList',
							name: 'relicList',
							meta: { role: "admin" },
							component: () => import('../views/admin/Relic/relicList.vue'),
						},
						{
							path: 'relicAdd',
							name: 'relicAdd',
							meta: { role: "admin" },
							component: () => import('../views/admin/Relic/relicAdd.vue'),
						},
						{
							path: 'relicBatchImport',
							name: 'relicBatchImport',
							meta: { role: "admin" },
							component: () => import('../views/admin/Relic/relicBatchImport.vue'),
						},
						{
							path: 'relicUpdate',
							name: 'relicUpdate',
							meta: { role: "admin" },
							component: () => import('../views/admin/Relic/relicUpdate.vue'),
						},
						{
							path: 'categories',
							name: 'categories',
							meta: { role: "admin" },
							component: () => import('../views/admin/Relic/categories.vue'),
							children:[
								{
									path:'/',
									redirect:"grouptypePage"
								},
								{
									path: 'grouptypePage',
									name: 'grouptypePage',
									meta: { role: "admin" },
									component: () => import('../views/admin/Relic/components/grouptypePage.vue'),
								},
								{
									path: 'localtypePage',
									name: 'localtypePage',
									meta: { role: "admin" },
									component: () => import('../views/admin/Relic/components/localtypePage.vue'),
								},
								{
									path: 'decadetypePage',
									name: 'decadetypePage',
									meta: { role: "admin" },
									component: () => import('../views/admin/Relic/components/decadetypePage.vue'),
								},
							]
						},
					]
				},
				{
					path: 'User',
					name: 'User',
					meta: { role: "admin" },
					component: () => import('../views/admin/User/user.vue'),
					children: [
						{
							path: 'userList',
							name: 'userList',
							meta: { role: "admin" },
							component: () => import('../views/admin/User/userList.vue'),
						},
						{
							path: 'userAdd',
							name: 'userAdd',
							meta: { role: "admin" },
							component: () => import('../views/admin/User/userAdd.vue'),
						},
						{
							path: 'userUpdate',
							name: 'userUpdate',
							meta: { role: "admin" },
							component: () => import('../views/admin/User/userUpdate.vue'),
						},
						{
							path: 'userCollection',
							name: 'userCollection',
							meta: { role: "admin" },
							component: () => import('../views/admin/User/userCollection.vue'),
						},
					]
				},
				{
					path: 'Analyse',
					name: 'Analyse',
					meta: { role: "admin" },
					component: () => import('../views/admin/Analyse/analyse.vue'),
					children: [
						{
							path: 'relicAnalyse',
							name: 'relicAnalyse',
							meta: { role: "admin" },
							component: () => import('../views/admin/Analyse/relicAnalyse.vue'),
						},
						{
							path: 'userAnalyse',
							name: 'userAnalyse',
							meta: { role: "admin" },
							component: () => import('../views/admin/Analyse/userAnalyse.vue'),
						}
					]
				},
			]
		},
	]
})

// 添加请求拦截器
router.beforeEach((to, from, next) => {
	let token = window.localStorage.getItem('token')
	let userRole = window.sessionStorage.getItem('role')
	//未登录，token还没有存储到本地
	if(token==null&&(to.name=='login'||to.name=='register')){
		next();
	}
	else{
		const timeOver = 8*3600 * 1000
		let date = new Date().getTime()
		let   tokenStatus=true //判断token过期状态
		const tokenStartTime = window.localStorage.getItem('tokenStartTime')
		//已经登录，判断token是否过期
		if(date - tokenStartTime > timeOver){
			tokenStatus=false  //记录token过期状态

		}
		//如果token过期了则转到登录页面
		if(!tokenStatus){
			if(to.name!=='login'){
				Message.error("登录状态过期，请重新登录")
				// console.log("tokenStatus1",tokenStatus)
				localStorage.removeItem('token')
				localStorage.removeItem('tokenStartTime')
				if(to.name=='register') next()
			    else next('/')
			}else{
				next()
			}	
		} //token没过期则放行
		else{
			if (to.name == 'login') {
				Message.info("您已登录，无须重复登录！")
				setTimeout(()=>{
				if(userRole=="user")
					return next('/Home')
				 else if(userRole=="admin")
					 return next('/AdminHome')
				},3000)
				
			}else{
				if(to.meta.role==window.sessionStorage.getItem('role'))
			     	next()
				else{
					console.log(to,from)
					Message("暂无访问权限！即将为您返回主页面~~~~~~")
					setTimeout(()=>{
						if(userRole=="user")
							return next('/Home')
						 else if(userRole=="admin"){
							 return next('/AdminHome')
						 }
					},3000)
				}

			}
		}

	}
})
  

//暴露router
export default router